<template>
	<view>
		<view class="p_text_c wrapper" v-if="!accessToken">
			<view class="p_font_36">首次使用需先授权登录百度网盘</view>
			<view class="p_font_32">1.点击“打开对话框”按钮</view>
			<view class="p_font_32">2.在对话框发送数字“<text class="p_color_fe8f0d p_bold">1</text>”</view>
			<view class="p_font_32">3.点链接登录百度网盘</view>
			<button plain open-type="contact" class="p_btn">
				<view>打开对话框</view>
			</button>
		</view>
		<view class="p_b_128 p_bg_all" :style="'padding-top: 140rpx'" v-else>
			<noneData noneTitle="暂无数据~" v-if="listData.length === 0"></noneData>
			<view v-else>
				<view class="top_wrapper">
					<view class="top_btn p_font_26 p_margin_t_12" @click="getBack">退出网盘</view>
					<view class="p_flex2 p_one_text p_flex_2" style="margin: 24rpx 0;">
						<view class="p_font_26 p_flex2" v-for="(item, index) in titleList" :key="index">
							<view class="p_font_26 p_color_b2" style="margin: 0 12rpx;" v-if="index != 0"> > </view>
							<view class="p_one_text" style="max-width: 300rpx;" @click="getTitle(item, index)">
								{{item.title}}
							</view>
						</view>
					</view>
				</view>
				<view class="main">
					<view class="p_flex9 list_wrapper p_font_28" v-for="(item, index) in listData" :key="index"
						@click="getTabList(item)">
						<view class="p_flex_1 p_one_text">{{item.server_filename}}</view>
						<view v-if="item.isdir == 1" class="p_margin_l_24 p_color_b2">点击展开</view>
						<image src="https://dy.sykj365.com/uploads/mini/weixuanzhong@2x.png" class="p_img_48 p_margin_l_24"
							v-if="!item.choose && item.isdir == 0"></image>
						<image src="https://dy.sykj365.com/uploads/mini/xuanzhong@2x.png" class="p_img_48 p_margin_l_24"
							v-if="item.choose && item.isdir == 0"></image>
					</view>
				</view>
			</view>
			<view class="p_bottom_fix">
				<view class="p_btn" @click="getSure">
					<view>确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import noneData from '../../components/noneData.vue';

	export default {
		components: {
			noneData
		},
		data() {
			return {
				accessToken: '',
				listData: [],
				oneListData: [],
				titleList: [{
					title: '我的网盘'
				}],
				chooseIds: [], // 选中的文件id
				chooseFiles: [],
			}
		},
		onLoad() {
			if (uni.getStorageSync('accessToken')) {
				this.accessToken = uni.getStorageSync('accessToken');
				this.getListData();
			} else {
				this.accessToken = '';
			}

			// this.accessToken = '123.f4995c517ef2e4b393539b51afac1103.YBSs382mxl32r5Q6lOWnbGJm18A-oo8kYN58zge.0j9sWg';
			// this.getListData();
		},
		methods: {
			getListData() {
				uni.showLoading();
				let self = this;
				let url_ = 'https://pan.baidu.com/rest/2.0/xpan/file?method=list&access_token=' + this.accessToken;
				uni.request({
					url: url_,
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: (res) => {
						uni.hideLoading();
						if (res.data.errno === 0) {
							let arr = [];
							if (res.data.list.length > 0) {
								res.data.list.forEach((n) => {
									n.size_ = (n.size / 1024 / 1024).toFixed(2)
									n.choose = false
									arr.push(n)
								})
							}
							self.oneListData = arr; // 一级目录
							self.listData = arr;
						} else if (res.data.errno === -6) {
							getApp().getMsg('登录失效');
							uni.removeStorageSync('accessToken');
							self.accessToken = '';
						}
					},
					fail: () => {
						uni.hideLoading();
					}
				})
			},
			getTabList(item) {
				if (item.isdir == 0) {
					// 文件
					let a_ = item.server_filename.split('.');
					if (a_[a_.length - 1] !== 'pdf' && a_[a_.length - 1] !== 'doc' && a_[a_.length - 1] !== 'docx' && a_[a_
							.length - 1] !== 'ppt' && a_[a_.length - 1] !== 'pptx') {
						getApp().getMsg('支持pdf、word、ppt格式')
					} else {
						item.choose = !item.choose;
					}
				}

				if (item.isdir == 1) {
					// 目录
					this.titleList.push({
						title: item.server_filename,
						path: item.path
					})
					this.getIsDirList(item);
				}
			},
			getTitle(item, index) {
				this.titleList.splice(index + 1, this.titleList.length - index + 1);
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 100
				})
				if (index == 0) {
					this.listData = this.oneListData;
					return
				}
				if (item.path) {
					this.getIsDirList(item);
				}
			},
			getIsDirList(item) {
				uni.showLoading();
				let self = this;
				let url_ = 'https://pan.baidu.com/rest/2.0/xpan/multimedia?method=listall&access_token=' + this
					.accessToken + '&path=' + item.path;
				uni.request({
					url: url_,
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: (res) => {
						uni.hideLoading();
						if (res.data.errno === 0) {
							let arr = [];
							if (res.data.list.length > 0) {
								res.data.list.forEach((n) => {
									n.size_ = (n.size / 1024 / 1024).toFixed(2)
									n.choose = false
									arr.push(n)
								})
							}
							self.listData = arr;
						}
					},
					fail: () => {
						uni.hideLoading();
					}
				})
			},
			getSure() {
				let ids = [];
				this.listData.forEach((n) => {
					if (n.choose) {
						ids.push(n.fs_id)
					}
				})

				if (ids.length === 0) return getApp().getMsg('请选择文件');

				this.chooseIds = ids;
				uni.showLoading();
				let url_ = 'https://pan.baidu.com/rest/2.0/xpan/multimedia?method=filemetas&access_token=' + this
					.accessToken;
				uni.request({
					url: url_,
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						dlink: 1,
						fsids: ids
					},
					success: (res) => {
						uni.hideLoading();
						this.chooseFiles = [];
						if (res.data.errno === 0) {
							res.data.list.forEach((n, k) => {
								this.getUpUrl(n, k)
							})
						}
					},
					fail: () => {
						uni.hideLoading();
					}
				})
			},
			getUpUrl(e, i) {
				let self = this;
				let d_ = e.filename.split('.');

				if (e.size / 1024 / 1024 > 500) {
					getApp().getMsg('文件最大不超过500M');
					return;
				}

				this.$http.post(this.$api.uploadUrl, {
					url: e.dlink + '&access_token=' + this.accessToken,
					name: e.filename,
					type: d_[d_.length - 1]
				}).then(res => {
					this.chooseFiles.push({
						name: e.filename,
						path: res.data
					});
					if (Number(i) == this.chooseIds.length - 1) {
						let a_ = this.chooseFiles;
						uni.navigateBack();
						uni.setStorageSync('baiduFiles', a_);
					}
				})
			},
			getBack() {
				uni.navigateBack();
				uni.removeStorageSync('accessToken');
			}
		}
	}
</script>

<style lang="less">
	.wrapper {
		padding: 80rpx 0 0 0;
	}

	.wrapper view {
		margin: 24rpx 0;
	}

	.wrapper .p_btn {
		margin-top: 70rpx;
	}

	.print_list {
		padding: 32rpx;
		margin: 32rpx 0;
		background-color: #fff;
		border-radius: 16rpx;
	}

	.top_wrapper {
		overflow: hidden;
		background-color: #fff;
		padding: 0 32rpx;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
	}

	.top_btn {
		color: #fff;
		width: 110rpx;
		text-align: center;
		padding: 4rpx 12rpx;
		border-radius: 10rpx;
		background-color: #05B570;
	}

	.main {
		width: 686rpx;
		margin: 20rpx auto;
		background-color: #fff;
		border-radius: 36rpx;
	}

	.list_wrapper {
		padding: 32rpx;
		border-bottom: 1px solid #f4f4f4;

		&:last-child {
			border-bottom: none;
		}
	}
</style>
